package com.metaverse.backend.service;

import com.metaverse.backend.config.SysConstants;
import com.metaverse.backend.domain.SysConfig;
import com.metaverse.backend.dto.PageQuery;
import com.metaverse.backend.exception.BusinessException;
import com.metaverse.backend.repo.SysConfigRepo;
import com.metaverse.backend.utils.JpaUtils;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

@Service
@AllArgsConstructor
public class SysConfigService {
    private SysConfigRepo sysConfigRepo;

    private final RedisTemplate<String, Object> redisTemplate;


    public Page<SysConfig> all(PageQuery pageQuery) {
        return sysConfigRepo.findAll(JpaUtils.toSpecification(pageQuery, SysConfig.class), JpaUtils.toPageRequest(pageQuery));
    }

    public BigDecimal getBigDecimal(String name) {
        return sysConfigRepo.findByName(name).map(sysConfig -> new BigDecimal(sysConfig.getValue()))
                .orElse(BigDecimal.ZERO);
    }

    public LocalTime getTime(String name) {
        String str = sysConfigRepo.findByName(name).map(SysConfig::getValue)
                .orElseThrow(new BusinessException("配置不存在"));
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm");
        return LocalTime.from(dateTimeFormatter.parse(str));
    }

    public long getDateTime(String name) {
        String str = sysConfigRepo.findByName(name).map(SysConfig::getValue)
                .orElseThrow(new BusinessException("配置不存在"));
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sf.parse(str).getTime();
        }catch (Exception e){
            throw  new BusinessException("检查【"+name+"】配置是否存在错误");
        }
    }


    public boolean getBoolean(String name) {
        String str = sysConfigRepo.findByName(name).map(SysConfig::getValue)
                .orElseThrow(new BusinessException("配置不存在"));
        return str.equals("1");
    }

    public int getInt(String name) {
        String str = sysConfigRepo.findByName(name).map(SysConfig::getValue)
                .orElseThrow(new BusinessException("配置不存在"));
        return Integer.parseInt(str);
    }

    public String getString(String name) {
        String str = sysConfigRepo.findByName(name).map(SysConfig::getValue)
                .orElseThrow(new BusinessException("配置不存在"));
        return str;
    }

    public String getStringDefault(String name) {
        String str = sysConfigRepo.findByName(name).map(SysConfig::getValue).orElse("");
        return str;
    }

    @PostConstruct
    public void init() {

        if (!sysConfigRepo.findByName(SysConstants.ORDER_EXPIRE_INTERVAL).isPresent()) {
            sysConfigRepo.save(SysConfig.builder()
                    .name(SysConstants.ORDER_EXPIRE_INTERVAL)
                    .desc("订单失效时间间隔（分钟）")
                    .type(SysConfig.ValueType.NUMBER)
                    .value("5")
                    .build());
        }

        if (!sysConfigRepo.findByName(SysConstants.MEMBER_PREFIX).isPresent()) {
            sysConfigRepo.save(SysConfig.builder()
                    .name(SysConstants.MEMBER_PREFIX)
                    .desc("用户名前缀")
                    .type(SysConfig.ValueType.STRING)
                    .value("")
                    .build());
        }








    }




}
